package com.heima.datasync.service.impl;

import com.heima.datasync.mapper.ApArticleMapper;
import com.heima.datasync.service.EsDataService;
import com.heima.es.service.EsService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.vos.SearchArticleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EsDataServiceImpl implements EsDataService {

    //索引库名称
    public static final String APP_INFO_ARTICLE = "app_info_article";
    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private EsService<SearchArticleVO>  esService;


    @Override
    public ResponseResult dataInit() {
        //判断索引库是否存在
        boolean isExist = esService.existIndex(APP_INFO_ARTICLE);

        if(isExist){
            esService.deleteIndex(APP_INFO_ARTICLE);
        }
        esService.createIndex(
                "{\n" +
                        "    \"mappings\":{\n" +
                        "        \"properties\":{\n" +
                        "            \"id\":{\n" +
                        "                \"type\":\"long\"\n" +
                        "            },\n" +
                        "            \"publishTime\":{\n" +
                        "                \"type\":\"date\"\n" +
                        "            },\n" +
                        "            \"layout\":{\n" +
                        "                \"type\":\"integer\"\n" +
                        "            },\n" +
                        "            \"images\":{\n" +
                        "                \"type\":\"keyword\",\n" +
                        "                \"index\": false\n" +
                        "            },\n" +
                        "           \"staticUrl\":{\n" +
                        "                \"type\":\"keyword\",\n" +
                        "                \"index\": false\n" +
                        "            },\n" +
                        "            \"authorId\": {\n" +
                        "          \t\t\"type\": \"long\"\n" +
                        "       \t\t},\n" +
                        "          \"title\":{\n" +
                        "            \"type\":\"text\",\n" +
                        "            \"analyzer\":\"ik_smart\"\n" +
                        "          }\n" +
                        "        }\n" +
                        "    }\n" +
                        "}",APP_INFO_ARTICLE);

        //批量导入数据
        //先查询第一页文章数据
        List<ApArticle> allArticles = apArticleMapper.findAllArticles();
        //判断数据是否存在
        if (allArticles==null||allArticles.size()==0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        //遍历所有数据  将数据封装到查询vo 并将数据存入集合
        List<SearchArticleVO> list = new ArrayList<>();
        for (ApArticle allArticle : allArticles) {
            //创建vo对象
            SearchArticleVO searchArticleVO = new SearchArticleVO();
            //COPY
            BeanUtils.copyProperties(allArticle,searchArticleVO);
            //装进集合中
            list.add(searchArticleVO);
        }
        //将数据保存致es
        esService.saveBatch(list,APP_INFO_ARTICLE);

        return ResponseResult.okResult();
    }
}
